package fluxx.draft;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public abstract class AbstractCardSet implements Iterable<Card> {

	protected LinkedList<Card> cards = new LinkedList<Card>();

	@Override
	public Iterator<Card> iterator() {
		return cards.iterator();
	}

	public Card get(int index) {
		return cards.get(index);
	}

	public int size() {
		return cards.size();
	}

	public boolean contains(Card card) {
		return cards.contains(card);
	}

	public boolean contains(String cardName) {
		for (Card card : cards) {
			if (card.getName() == cardName)
				return true;
		}
		return false;
	}

	public String toString() {
		int size = size();
		if (size == 0) return "";
		if (size == 1) return ""+get(0);
	
		StringBuffer buffer = new StringBuffer();
		int cardsLeft = size;
		for (Card card : cards) {
			cardsLeft--;
			if (cardsLeft == 1) buffer.append(card);
			else if (cardsLeft == 0) buffer.append(" and "+card);
			else buffer.append(card+", ");
		}
		return ""+buffer;
	}

}
